package com.tarena.lbs.common.marketing.converter;


import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;

import com.tarena.lbs.common.marketing.enums.ActivityStatusEnum;
import com.tarena.lbs.common.marketing.enums.EnableStatusEnum;
import com.tarena.lbs.common.marketing.enums.MarketingResultEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.marketing.bo.CouponBO;
import com.tarena.lbs.pojo.marketing.dos.CouponDO;
import com.tarena.lbs.pojo.marketing.param.CouponParam;
import com.tarena.lbs.pojo.marketing.query.CouponQuery;
import com.tarena.lbs.pojo.marketing.vo.CouponVO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CouponConverter {

    public PageResult<CouponVO> assemblePageVOS(BasePageList<CouponBO> bos, CouponQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<CouponVO> result = new PageResult<>();
        List<CouponVO> vos = new ArrayList<>();
        for (CouponBO temp : bos.getList()) {
            CouponVO vo = new CouponVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }

    public CouponDO param2DO(CouponParam param) throws BusinessException {
        CouponDO couponDO = new CouponDO();
        BeanUtils.copyProperties(param, couponDO);
        couponDO.setCreateAt(new Date());
        couponDO.setStatus(0);
        couponDO.setCouponType(1);
        try {
            couponDO.setEndDate(DateUtils.parseDate(param.getEndDate(),"yyyy-MM-dd HH:mm:ss"));
            couponDO.setStartDate(DateUtils.parseDate(param.getStartDate(),"yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            throw new BusinessException(MarketingResultEnum.DATE_ERROR);
        }
        couponDO.setEnableStatus(EnableStatusEnum.DISABLE.getCode());
        if (couponDO.getStartDate().before(new Date()) && couponDO.getEndDate().after(new Date())) {
            couponDO.setStatus(ActivityStatusEnum.ENABLE.getCode());
        }
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            couponDO.setBusinessId(loginUser.getId().intValue());
            param.setBusinessId(loginUser.getId().intValue());
        }
        return couponDO;
    }

    public CouponVO bo2vo(CouponBO bo) {
        if (bo == null) {
            return null;
        }
        CouponVO vo = new CouponVO();
        BeanUtils.copyProperties(bo,vo);
        return vo;
    }

    public CouponBO do2bo(CouponDO tempDo) {
        if (tempDo == null) {
            return null;
        }
        CouponBO bo = new CouponBO();
        BeanUtils.copyProperties(tempDo,bo);
        return bo;
    }
}
